#!/bin/sh

if test "$1" != "start"
then
    # call user script with stop condition
    test -e /boot/postshare.sh && bash /boot/postshare.sh stop
    exit 0
fi

DOUPDATE=0
CURVERSION=$(cat /usr/share/batocera/batocera.version)
SHAREVERSION=$(cat /userdata/system/data.version 2>/dev/null)
test "${CURVERSION}" != "${SHAREVERSION}" && DOUPDATE=1 # skip some part, just when new release

IN=/usr/share/batocera/datainit
OUT=/userdata
# force the creation of some directories
for DIR in cheats \
           system/.cache \
           system/.config/lirc \
           system/bluetooth \
           system/configs/retroarch/config/remaps \
           system/configs/retroarch/inputs \
           system/pacman \
           system/pacman/db \
           system/pacman/pkg \
           system/pacman/hooks \
           system/logs
do
    if test ! -e "$OUT""/""$DIR"
    then
        mkdir -p "$OUT""/""$DIR"
    fi
done

# directories to force directories exists
# files are linked
#
# ONLY NEW DIRECTORIES ARE UPDATED WHEN batocera.linux IS UPDATED
#
# the user can't delete any directory, otherwise, it will recreate it, with link inside
# the user can delete links and add its own files
# to reset the directory or update it, the user has to remove the directory (we are unable to differenciate new files from batocera.linux update from a file removed by the user)
for DIR in roms roms/mame/mame2003
do
    if mkdir -p "$OUT""/""$DIR"
    then
	find "$IN""/""$DIR" -mindepth 1 -maxdepth 1 -type d |
	    while read SUBDIR
	    do
		BNAME=$(basename "$SUBDIR")
		TDIR="$OUT""/""$DIR""/""$BNAME"
		if test ! -e "$TDIR" # ASSUME THAT PARENTS ARE LISTED BEFORE CHILDREN
		then
		    cp -r "$SUBDIR" "$TDIR"
		fi
	    done
    fi
done

# sometimes we update _info.txt files.
# add a loop to update newer files if necessary
if test "${DOUPDATE}" = 1
then
    for DIR in roms roms/mame/mame2003
    do
	find "$IN""/""$DIR" -mindepth 1 -maxdepth 2 -type f -name "_info.txt" |
            while read FILE
            do
		RELATIVE_PATH="${FILE#$IN/}"
		DEST_FILE="$OUT/$RELATIVE_PATH"
            
		# Check if the source file is newer than the destination file
		if [ "$FILE" -nt "$DEST_FILE" ]; then
                    echo "Updating $DEST_FILE"
                    cp -f "$FILE" "$DEST_FILE"
		fi
            done
    done
fi

# directories / files to copy when initializing the user data
#
# NO UPDATE IS POSSIBLE ON THESE DIRECTORIES WHEN batocera.linux IS UPDATED
#
# there are almost empty, it's just for the structure
# the user can delete the directory, it will recreate the structure
# the user can delete files, files will not be recreated
for FILE in music bios extractions kodi library saves screenshots recordings splash \
            system/.config/lirc/lircd.conf \
            system/configs/emulationstation/es_settings.cfg \
            system/.kodi \
            system/configs/amiberry \
            system/configs/amiberry/whdboot \
            system/configs/amiberry/data \
            system/configs/cannonball \
            system/configs/dosbox \
            system/configs/mupen64 \
            system/configs/ppsspp \
            system/configs/shadersets \
            system/configs/umtool \
            system/configs/theforceengine \
            system/batocera.conf \
            system/pacman/hooks/batocera-install.hook \
            system/pacman/hooks/batocera-uninstall.hook \
            system/pacman/pacman.conf \
            bios/mame2003 \
            bios/dolphin-emu \
            roms/iortcw/main \
            bios/ps2 \
            system/configs/pironman5/config.json \
            bios/commanderx16
do
    # Skip updating specific files
    if [ "$FILE" = "system/configs/emulationstation/es_settings.cfg" ] || [ "$FILE" = "system/batocera.conf" ] || [ "$FILE" = "system/.kodi/userdata/guisettings.xml" ]; then
        # Check if the file doesn't exist, then create it
        if [ ! -e "${OUT}/${FILE}" ]; then
            mkdir -p "$(dirname "${OUT}/${FILE}")"
            echo "Creating $OUT/$FILE"
            cp -rfu --preserve=timestamps "${IN}/${FILE}" "$(dirname "${OUT}/${FILE}")"/
        fi
    else
        # Update if newer
        if [ -e "${IN}/${FILE}" ] && { [ ! -e "${OUT}/${FILE}" ] || [ "${IN}/${FILE}" -nt "${OUT}/${FILE}" ]; }; then
            mkdir -p "$(dirname "${OUT}/${FILE}")"
            echo "Updating $OUT/$FILE"
            cp -rfu --preserve=timestamps "${IN}/${FILE}" "$(dirname "${OUT}/${FILE}")"/
        fi
    fi
done

### specific directories
# decorations - copy only the readme file
mkdir -p "${OUT}/decorations"
test -e "${OUT}/decorations/readme.txt" || cp -r "${IN}/decorations/readme.txt" "${OUT}/decorations/readme.txt"

# themes
mkdir -p "${OUT}/themes"

# ssh : create directory, but not keys because lack of entropy, dropbear will automatically generate key on client connection
mkdir -p /userdata/system/ssh

# ssh : create directory for SSH user data (including identity keys, authorized_keys, known_hosts)
mkdir -p -m 0700 /userdata/system/.ssh
chmod 0700 /userdata/system/.ssh

# and add the machine-id
test -e /userdata/system/machine-id || dbus-uuidgen --ensure=/userdata/system/machine-id
ln -sf /userdata/system/machine-id /var/lib/dbus/machine-id
ln -sf /userdata/system/machine-id /etc/machine-id

# save to avoid to redo that all the times
cp /usr/share/batocera/batocera.version /userdata/system/data.version

# call user script with start condition
test -e /boot/postshare.sh && bash /boot/postshare.sh start

exit 0
# END
